package jgl.vajra.kingkong.part.mine.activity;

import android.text.TextUtils;

import org.greenrobot.eventbus.EventBus;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.databinding.ActivityNameAuthenticationBinding;
import jgl.vajra.kingkong.event.ModifyInformationEvent;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.part.mine.contract.NameAuthenticationContract;
import jgl.vajra.kingkong.part.mine.viewModel.NameAuthenticationViewModel;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(NameAuthenticationViewModel.class)
public class NameAuthenticationActivity extends BaseMVVMActivity<NameAuthenticationViewModel, ActivityNameAuthenticationBinding> implements NameAuthenticationContract.View {
    @Override
    public int getLayoutId() {
        return R.layout.activity_name_authentication;
    }

    @Override
    public void initView() {
        super.initView();
        mBinding.setView(this);

        String name = getIntent().getStringExtra("name");
        mBinding.etName.setText(name);

        String idCardNumber = getIntent().getStringExtra("idCardNumber");
        mBinding.etIdNumber.setText(AEACCoder.decrypt(idCardNumber));

    }

    //保存
    @Override
    public void onDetermine() {
        String name = mBinding.etName.getText().toString().trim();
        if (TextUtils.isEmpty(name)) {
            ToastUtils.showShort("请输入真实姓名");
            return;
        }
        String idNumber = mBinding.etIdNumber.getText().toString().trim();
        if (TextUtils.isEmpty(idNumber)) {
            ToastUtils.showShort("请输入身份证号");
            return;
        }

        Map<String, Object> map = new HashMap<>();
        map.put("Authorization", AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization);
        map.put("name", URLEncoder.encode(name));
        map.put("idCardNumber", AEACCoder.encrypt(idNumber));

        mViewModel.modifyInformation(map);

    }

    @Override
    public void returnModifyInformationData() {
        ToastUtils.showShort("修改成功");
        EventBus.getDefault().post(new ModifyInformationEvent(1,""));
        finish();
    }
}
